package com.hmall.common.config;

import com.hmall.common.domain.R;
import com.hmall.common.exception.AssertException;
import com.hmall.common.exception.CommonException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

/*********************************************************
 **
 ** <br><br>
 ** @ClassName: GlobalExceptionHandler
 ** @author: yangfeng
 ** @date: 2024/12/19 10:48
 ** @version: 1.0.0
 *********************************************************/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler({AssertException.class, CommonException.class,IllegalArgumentException.class})
    public R handlerAssertException(Exception ex) {
        log.error("发生业务异常", ex);
        return R.error(ex.getMessage());
    }
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R handleSQLIntegrityConstraintViolationException(Exception ex){
        log.error("发生业务异常", ex);
        if(ex.getMessage().contains("Duplicate entry")){
            return R.error("数据已存在");
        }
        return R.error(ex.getMessage());
    }
}
